

#include "timer.h"

/*
函数功能：初始化TimA开启定时中断功能
输入：
    uint8_t Tim_Ax ：选择定时器A_x，可选0 1 2 3
    uint8_t Tim_Ax_x： 选择定时器通道
    uint8_t  CLK_DIV：时钟分频，可选0-64
    uint16_t Per    ：定时器周期，可选0-65535
输出：无
备注：定时时间计算：
    Time=(CLK_DIV*Per)/时钟频率
*/
void    Timer_init(uint8_t Tim_Ax,uint8_t Tim_Ax_x,uint8_t  CLK_DIV,uint16_t Per )
{
    // uint8_t Tim_A_x;
    uint32_t baseAddress;
    uint16_t ch;
    baseAddress=(PERIPH_BASE+(uint32_t)((Tim_Ax*4)<<8));
    ch=(0x02+(uint16_t)(0x02*Tim_Ax_x));
    Timer_A_UpDownModeConfig up_cing_status;
    up_cing_status.clockSource=TIMER_A_CLOCKSOURCE_SMCLK;//时钟源选择48MHz
    up_cing_status.clockSourceDivider= CLK_DIV ;                         //分频选择0-64 
    up_cing_status.timerPeriod=Per;               //自动重装载值           
    up_cing_status.captureCompareInterruptEnable_CCR0_CCIE=TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;//使能CCR0更新中断
    up_cing_status.timerClear=  TIMER_A_DO_CLEAR;//清除计数器值
    up_cing_status.timerInterruptEnable_TAIE=TIMER_A_TAIE_INTERRUPT_DISABLE;//失能Tim溢出中断
    Timer_A_configureUpDownMode(baseAddress ,&up_cing_status);//初始化定时器
    Timer_A_startCounter(baseAddress ,TIMER_A_UP_MODE);//开始计数
    // Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE ,TIMER_A_CAPTURECOMPARE_REGISTER_0 );
    Timer_A_clearCaptureCompareInterrupt(baseAddress ,ch );//清除中断标志位
    Interrupt_enableInterrupt(INT_TA0_0 );//开启端口中断
    
    
}

